篇首语:本文由编程笔记#小编为大家整理,主要介绍了Spring Boot 2.x 实践记:Mail相关的知识,希望对你有一定的参考价值。
本文是实践如何在Spring Boot 2
中使用JavaMailSender
发送邮件:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
<version>2.2.6.RELEASEversion>
dependency>
2. SMTP配置
spring.mail.host&#61;smtp.exmail.qq.com
spring.mail.username&#61;xxx&#64;xxx.com
spring.mail.password&#61;xxxxxx
spring.mail.properties.mail.smtp.auth&#61;true
# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable&#61;true
spring.mail.properties.mail.smtp.starttls.required&#61;true
spring.mail.host&#61;smtp.gmail.com
spring.mail.port&#61;25
spring.mail.username&#61;xxx&#64;gmail.com
spring.mail.password&#61;xxxxxx
spring.mail.properties.mail.smtp.auth&#61;true
spring.mail.properties.mail.smtp.connectiontimeout&#61;5000
spring.mail.properties.mail.smtp.timeout&#61;5000
spring.mail.properties.mail.smtp.writetimeout&#61;5000
# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable&#61;true
spring.mail.properties.mail.smtp.starttls.required&#61;true
如果是内部服务器&#xff0c;需要由管理员提供基础信息。
spring.mail.host&#61;smtp-mail.outlook.com
spring.mail.port&#61;587
spring.mail.username&#61;xxx&#64;outlook.com
spring.mail.password&#61;xxxxxx
spring.mail.properties.mail.protocol&#61;smtp
spring.mail.properties.mail.tls&#61;true
spring.mail.properties.mail.smtp.auth&#61;true
spring.mail.properties.mail.smtp.starttls.enable&#61;true
spring.mail.properties.mail.smtp.ssl.trust&#61;smtp-mail.outlook.com
3. 发送简单邮件
&#64;Resource
private JavaMailSender mailSender;
public void sendSimpleMail()
SimpleMailMessage message &#61; new SimpleMailMessage();
message.setFrom("xxx&#64;xxx.com");
message.setTo("xxx&#64;xxx.com");
message.setSubject("主题&#xff1a;简单邮件");
message.setText("这是一封简单邮件");
try
mailSender.send(message);
catch (Exception e)
e.printStackTrace();
4. 发送带附件邮件
&#64;Resource
private JavaMailSender mailSender;
public void sendAttachmentsMail()
MimeMessage mimeMessage &#61; mailSender.createMimeMessage();
try
MimeMessageHelper helper &#61; new MimeMessageHelper(mimeMessage, true);
helper.setFrom("xxx&#64;xxx.com");
helper.setTo("xxx&#64;xxx.com");
helper.setSubject("主题&#xff1a;带有附件的邮件");
helper.setText("有附件");
FileSystemResource file &#61; new FileSystemResource(new File("/data/test.xlsx"));
helper.addAttachment("test.xlsx", file);
mailSender.send(mimeMessage);
catch (Exception e)
e.printStackTrace();
5. 发送HTML格式邮件
&#64;Resource
private JavaMailSender mailSender;
public Boolean sendHtmlMail()
MimeMessage mimeMessage &#61; mailSender.createMimeMessage();
try
MimeMessageHelper helper &#61; new MimeMessageHelper(mimeMessage, true);
helper.setFrom("xxx&#64;xxx.com");
helper.setTo("xxx&#64;xxx.com");
helper.setSubject("主题&#xff1a;带有html的邮件");
helper.setText("", true);
FileSystemResource file &#61; new FileSystemResource(new File("/web/test.jpg"));
helper.addInline("test.jpg", file);
mailSender.send(mimeMessage);
catch (Exception e)
e.printStackTrace();
6. 实战
在实际邮件发送中&#xff0c;发送者通常为1个&#xff0c;而接受者通常是多个&#xff0c;而且还有抄送或密送&#xff0c;下面我们就来基于上面邮件发送的基本实现&#xff0c;来扩展实战一下。
package com.mickjoust.demo.springboot2_in_action.mail;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* &#64;author mickjoust
**/
&#64;SpringBootApplication
public class AppStartMail
public static void main(String[] args)
SpringApplication.run(AppStartMail.class,args);
为了方便模拟传参数&#xff0c;我们创建一个简单的邮件对象。
package com.mickjoust.demo.springboot2_in_action.mail;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* &#64;author mickjoust
**/
&#64;Data
&#64;AllArgsConstructor
&#64;NoArgsConstructor
public class MailInfo
private String subject;
private String content;
private String from;
private String[] to;
private String[] cc;
private String[] bcc;
private String fileName;
private String filePath;
分别提供三个接口&#xff0c;接受发送邮件&#xff0c;如果发送成功则返回成功&#xff0c;否则返回失败。
package com.mickjoust.demo.springboot2_in_action.mail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
/**
* &#64;author mickjoust
**/
&#64;Slf4j
&#64;RestController
public class TestSendMailController
&#64;Resource
private SendMailService sendMailService;
&#64;PostMapping("/mail/simple")
public String sendSimpleMail(&#64;RequestBody MailInfo mailInfo)
if (sendMailService.sendSimpleMail(mailInfo))
return "true";
return "false";
&#64;PostMapping("/mail/attachment")
public String sendAttachmentsMail(&#64;RequestBody MailInfo mailInfo)
if (sendMailService.sendAttachmentsMail(mailInfo))
return "true";
return "false";
&#64;PostMapping("/mail/html")
public String sendHtmlMail(&#64;RequestBody MailInfo mailInfo)
if (sendMailService.sendHtmlMail(mailInfo))
return "true";
return "false";
package com.mickjoust.demo.springboot2_in_action.mail;
/**
* &#64;author mickjoust
**/
public interface SendMailService
Boolean sendSimpleMail(MailInfo mailInfo);
Boolean sendAttachmentsMail(MailInfo mailInfo);
Boolean sendHtmlMail(MailInfo mailInfo);
package com.mickjoust.demo.springboot2_in_action.mail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
/**
* &#64;author mickjoust
**/
&#64;Slf4j
&#64;Service("sendMailService")
public class SendMailServiceImpl implements SendMailService
&#64;Resource
private JavaMailSender mailSender;
private boolean isEmpty(final String[] array)
return array &#61;&#61; null || array.length &#61;&#61; 0;
private boolean isNotEmpty(final String[] array)
return !isEmpty(array);
/**
* 发送普通邮件
* &#64;param subject 主题
* &#64;param content 内容
* &#64;param from 发自
* &#64;param to 发送
* &#64;param cc 抄送
* &#64;param bcc 密送
* &#64;return 返回
*/
public Boolean sendSimpleMail(String subject, String content, String from, String[] to,String[] cc,String[] bcc)
SimpleMailMessage message &#61; new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
if (isNotEmpty(cc))
message.setCc(cc);
if (isNotEmpty(bcc))
message.setBcc(bcc);
message.setSubject(subject);
message.setText(content);
try
mailSender.send(message);
return true;
catch (Exception e)
log.error("&#61;&#61;&#61;发送邮件异常",e);
return false;
/**
* 发送附件邮件
* &#64;param subject
* &#64;param content
* &#64;param from
* &#64;param fillepath /web/test.xlsx
* &#64;param filename test.xlsx
* &#64;param to
* &#64;param cc
* &#64;param bcc
* &#64;return
*/
public Boolean sendAttachmentsMail(String subject,String content,String from, String fillepath, String filename,
String[] to,
String[] cc,
String[] bcc)
MimeMessage mimeMessage &#61; mailSender.createMimeMessage();
try
MimeMessageHelper helper &#61; new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(to);
if (isNotEmpty(cc))
helper.setCc(cc);
if (isNotEmpty(bcc))
helper.setBcc(bcc);
helper.setSubject(subject);
helper.setText(content);
FileSystemResource file &#61; new FileSystemResource(new File(fillepath));
helper.addAttachment(filename, file);
catch (Exception e)
e.printStackTrace();
log.error("&#61;&#61;&#61;创建附件邮件异常", e);
try
mailSender.send(mimeMessage);
return true;
catch (Exception e)
log.error("&#61;&#61;&#61;发送邮件异常",e);
return false;
/**
* 发送富文本邮件
* &#64;param subject
* &#64;param content 比如&#xff1a;
* &#64;param from
* &#64;param fillepath 比如&#xff1a;/web/test.jpg
* &#64;param filename 比如&#xff0c;test
* &#64;param to
* &#64;param cc
* &#64;param bcc
* &#64;return
*/
public Boolean sendHtmlMail(String subject,String content,String from, String fillepath, String filename,
String[] to,
String[] cc,
String[] bcc)
MimeMessage mimeMessage &#61; mailSender.createMimeMessage();
try
MimeMessageHelper helper &#61; new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(to);
if (isNotEmpty(cc))
helper.setCc(cc);
if (isNotEmpty(bcc))
helper.setBcc(bcc);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file &#61; new FileSystemResource(new File(fillepath));
helper.addInline(filename, file);
catch (Exception e)
log.error("&#61;&#61;&#61;创建富文本邮件异常", e);
try
mailSender.send(mimeMessage);
return true;
catch (Exception e)
log.error("&#61;&#61;&#61;发送邮件异常",e);
return false;
&#64;Override
public Boolean sendSimpleMail(MailInfo mailInfo)
return sendSimpleMail(mailInfo.getSubject(),mailInfo.getContent(),mailInfo.getFrom(),
mailInfo.getTo(),mailInfo.getCc(),mailInfo.getBcc());
&#64;Override
public Boolean sendAttachmentsMail(MailInfo mailInfo)
return sendAttachmentsMail(mailInfo.getSubject(),mailInfo.getContent(),mailInfo.getFrom(),
mailInfo.getFilePath(),mailInfo.getFileName(),mailInfo.getTo(),mailInfo.getCc(),mailInfo.getBcc());
&#64;Override
public Boolean sendHtmlMail(MailInfo mailInfo)
return sendHtmlMail(mailInfo.getSubject(),mailInfo.getContent(),mailInfo.getFrom(),
mailInfo.getFilePath(),mailInfo.getFileName(),mailInfo.getTo(),mailInfo.getCc(),mailInfo.getBcc());
7. 测试
到此&#xff0c;我们就学会了在Spring Boot 2 中发送邮件的方法。